<?php

namespace app\api\controller\ykjp\product;

use app\common\controller\Backend;
use app\common\model\Category as CategoryModel;
use app\common\controller\Api;
use fast\Tree;


/**
 * 单位接口
 */
class Product extends Api
{

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';
   

    public function getProduct()
    {
        $this->model = new \app\admin\model\ykjp\product\Product;
        $row = $this->model->where('deletetime',null)->select();
        // $this->success('返回成功', ['data' => $row]);

        $categorydata= array();
        foreach ($row as $k => $v) {
                $categorydata[$k] = $v;
            if ($v['image']) {
                $categorydata[$k]['image'] = $this->request->scheme() . "://".$_SERVER["HTTP_HOST"] .$v['image'];
            }else{
                $categorydata[$k]['image'] = null;
            }
             
        }
        $this->success('返回成功', ['data' => $categorydata]);


    }


    public function deleteProduct()
    {
        $id = input('post.id');
        $row = \app\admin\model\ykjp\product\Product::where("id", $id)->delete();
        $this->success('返回成功', ['data' =>'删除成功！']);
    }

    //  添加
    public function add(){
        $name = input('post.name');
        $product_type_id = input('post.product_type_id');
        $specification = input('post.specification');
        $sku = input('post.sku');
        $prop = input('post.prop');
        $remark = input('post.remark');
        $product_unit_id = input('post.product_unit_id');
        $unit = input('post.unit');
        $image = input('post.image');

        $model = new \app\admin\model\ykjp\product\Product;
        $time = time();

        $model->insert([
            'createtime' => $time,
            'updatetime' => $time,
            'name' => $name,
            'product_type_id' => $product_type_id,
            'specification' => $specification,
            'sku' => $sku,
            'prop' => '',
            'remark' => $remark,
            'product_unit_id' => $product_unit_id,
            'image' => $image,
            'unit' => $unit
        ]);

        $this->success('返回成功', ['data' =>'添加成功！']);
    }


}
